﻿//************************************************************************
//* Written by Jirat Boomuang
//* Filename: AdaEntityUtils.cs 
//* Revision: 1.3
//* Author: JiratB
//* Platform: .NET Framework 4.0
//* Description: Dynamics CRM Entity utilities. You may change the 
//*              THAI_LANGUAGE_CODE constant to 0 should you wish to 
//*              execute it with the instance that doesn't have Thai
//*              language code. Otherwise, change it to 1054 to enable
//*              Thai labels.
//* Note: Ada is my lovely daughter.
//************************************************************************

//************************************************************************
//* Revision History
//------------------------------------------------------------------------
//* Revision  Date      Signature    Description
//------------------------------------------------------------------------
//* 1.0       20130908  JiratB       - First draft.
//* 1.1       20130927  JiratB       - Make it compatible with Orion and
//*                                    instance that doesn't have Thai
//*                                    language (1054).
//* 1.2       20131003  JiratB       - Change the default THAI_LANGUAGE_CODE
//*                                    to 0 (used to be 1054).
//* 1.3       20131114  JiratB       - Fix bug under CreateAnInteger()
//*                                    ensure that Max and Min assignment
//*                                    are taking "int32" as the input values
//************************************************************************

//-- For Standard Components ---------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;

using System.ServiceModel;
using System.ServiceModel.Description;

using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Discovery;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;

using Microsoft.Crm.Sdk.Messages;
//------------------------------------------------------------------------

namespace AdaCRMUtilities
{
    public static class AdaEntityConstants
    {
        #region Constants
        public const string HEADER_SCHEMANAME = "SchemaName";
        public const string HEADER_DISPLAYNAME_EN = "DisplayName_EN";
        public const string HEADER_DISPLAYCOLLECTIONAME_EN = "DisplayCollectionName_EN";
        public const string HEADER_DESCRIPTION_EN = "Description_EN";
        public const string HEADER_DISPLAYNAME_TH = "DisplayName_TH";
        public const string HEADER_DISPLAYCOLLECTIONNAME_TH = "DisplayCollectionName_TH";
        public const string HEADER_DESCRIPTION_TH = "Description_TH";
        public const string HEADER_OWNERSHIPTYPE = "OwnershipType";
        public const string HEADER_ISACTIVITY = "IsActivity";
        public const string HEADER_P_SCHEMANAME = "P_SchemaName";
        public const string HEADER_P_REQUIREDLEVEL = "P_RequiredLevel";
        public const string HEADER_P_MAXLENGTH = "P_MaxLength";
        public const string HEADER_P_FORMAT = "P_Format";
        public const string HEADER_P_DISPLAYNAME_EN = "P_DisplayName_EN";
        public const string HEADER_P_DESCRIPTION_EN = "P_Description_EN";
        public const string HEADER_P_DISPLAYNAME_TH = "P_DisplayName_TH";
        public const string HEADER_P_DESCRIPTION_TH = "P_Description_TH";

        public const string HEADER_F_ATTRIBUTEFILE = "AttributeFile";
        public const string HEADER_F_RELATIONSHIPFILEOTM = "RelationshipFileOTM";
        public const string HEADER_F_RELATIONSHIPFILEMTM = "RelationshipFileMTM";

        public const string HEADER_ATTRIBUTESCHEMANAME = "SchemaName";
        public const string HEADER_ATTRIBUTETYPE = "AttributeType";
        public const string HEADER_REQUIREDLEVEL = "RequiredLevel";
        //public const string HEADER_DISPLAYNAME_EN = "DisplayName_EN";
        //public const string HEADER_DISPLAYNAME_TH = "DisplayName_TH";
        //public const string HEADER_DESCRIPTION_EN = "Description_EN";
        //public const string HEADER_DESCRIPTION_TH = "Description_TH";
        public const string HEADER_MAXLENGTH = "MaxLength";
        public const string HEADER_STRINGFORMAT = "StringFormat";
        public const string HEADER_DATETIMEFORMAT = "DateTimeFormat";
        public const string HEADER_PRECISION = "Precision";
        public const string HEADER_MINIMUMVALUE = "MinimumValue";
        public const string HEADER_MAXIMUMVALUE = "MaximumValue";
        public const string HEADER_INTEGERFORMAT = "IntegerFormat";
        public const string HEADER_MEMOFORMAT = "MemoFormat";
        public const string HEADER_IMEMODE = "IMEMode";
        public const string HEADER_BOOLEAN00 = "Boolean00";
        public const string HEADER_BOOLEAN01 = "Boolean01";
        #endregion
    }//-- End of AdaEntityConstants class --

    public static class AdaEntityUtils
    {
        #region LIST constants
        //-- Change this to 1054 should you wish to utilize Thai MUI, otherwise change it to zero, JiratB, 20131030 --
        //-- In the case that you wish to use other language, change the value to your language code, JiratB, 20131114 -- 
        public const int THAI_LANGUAGE_CODE = 1054; 
        public const int ENGLISH_LANGUAGE_CODE = 1033;

        public const string LIST_REQUIREDLEVEL_NONE = "None";
        public const string LIST_REQUIREDLEVEL_RECOMMENDED = "Recommended";
        public const string LIST_REQUIREDLEVEL_REQUIRED = "Required";
        public const string LIST_REQUIREDLEVEL_SYSTEMREQUIRED = "SystemRequired";

        public const string LIST_STRINGFORMAT_EMAIL = "Email";
        public const string LIST_STRINGFORMAT_PHONE = "Phone";
        public const string LIST_STRINGFORMAT_TEXT = "Text";
        public const string LIST_STRINGFORMAT_TEXTAREA = "TextArea";
        public const string LIST_STRINGFORMAT_URL = "URL";

        public const string LIST_DATETIMEFORMAT_DATEANDTIME = "DateAndTime";
        public const string LIST_DATETIMEFORMAT_DATEONLY = "DateOnly";

        public const string LIST_IMEMODE_ACTIVE = "Active";
        public const string LIST_IMEMODE_AUTO = "Auto";
        public const string LIST_IMEMODE_DISABLED = "Disabled";
        public const string LIST_IMEMODE_INACTIVE = "Inactive";

        public const string LIST_ATTRIBUTETYPE_UNKNOWN = "Unknown";
        public const string LIST_ATTRIBUTETYPE_SINGLELINETEXT = "SingleLineText";
        public const string LIST_ATTRIBUTETYPE_OPTIONSET = "OptionSet";
        public const string LIST_ATTRIBUTETYPE_TWOOPTIONS = "TwoOptions";
        public const string LIST_ATTRIBUTETYPE_WHOLENUMBER = "WholeNumber";
        public const string LIST_ATTRIBUTETYPE_FLOATINGPOINT = "FloatingPoint";
        public const string LIST_ATTRIBUTETYPE_DECIMALNUMBER = "DecimalNumber";
        public const string LIST_ATTRIBUTETYPE_CURRENCY = "Currency";
        public const string LIST_ATTRIBUTETYPE_MULTIPLELINETEXT = "MultipleLineText";
        public const string LIST_ATTRIBUTETYPE_DATEANDTIME = "DateAndTime";
        public const string LIST_ATTRIBUTETYPE_LOOKUP = "Lookup";
        #endregion

        #region Conversion Methods
        public static AttributeRequiredLevel ToAttributeRequiredLevelManagedProperty(string input)
        {
            if (input == LIST_REQUIREDLEVEL_NONE)
                return AttributeRequiredLevel.None;
            else if (input == LIST_REQUIREDLEVEL_RECOMMENDED)
                return AttributeRequiredLevel.Recommended;
            else if (input == LIST_REQUIREDLEVEL_REQUIRED)
                return AttributeRequiredLevel.ApplicationRequired;
            else if (input == LIST_REQUIREDLEVEL_SYSTEMREQUIRED)
                return AttributeRequiredLevel.SystemRequired;
            else
            {
                throw new Exception(
                    String.Format("[ERROR], ToAttributeRequiredLevelManagedProperty(): An AttributeRequiredLevel is expected but {0} is set",
                    input)
                    );
            }
        }

        public static int ToInt(string input)
        {
            return Convert.ToInt32(input);
        }

        public static double ToDouble(string input)
        {
            return Convert.ToDouble(input);
        }

        public static decimal ToDecimal(string input)
        {
            return Convert.ToDecimal(input);
        }

        public static StringFormat ToStringFormat(string input)
        {
            if (input == LIST_STRINGFORMAT_EMAIL)
                return StringFormat.Email;
            //else if (input == LIST_STRINGFORMAT_PHONE)
            //    return StringFormat.Phone;
            else if (input == LIST_STRINGFORMAT_TEXT)
                return StringFormat.Text;
            else if (input == LIST_STRINGFORMAT_TEXTAREA)
                return StringFormat.TextArea;
            else if (input == LIST_STRINGFORMAT_URL)
                return StringFormat.Url;
            else
            {
                throw new Exception(
                    String.Format("[ERROR], ToStringFormat(): A StringFormat is expected but {0} is set",
                    input)
                    );
            }
        }

        public static DateTimeFormat ToDateTimeFormat(string input)
        {
            if (input == LIST_DATETIMEFORMAT_DATEANDTIME)
                return DateTimeFormat.DateAndTime;
            else if (input == LIST_DATETIMEFORMAT_DATEONLY)
                return DateTimeFormat.DateOnly;
            else
            {
                throw new Exception(
                    String.Format("[ERROR], ToDateTimeFormat(): A DateTimeFormat is expected but {0} is set",
                    input)
                    );
            }
        }

        public static ImeMode ToImeMode(string input)
        {
            if (input == LIST_IMEMODE_ACTIVE)
                return ImeMode.Active;
            else if (input == LIST_IMEMODE_AUTO)
                return ImeMode.Auto;
            else if (input == LIST_IMEMODE_DISABLED)
                return ImeMode.Disabled;
            else if (input == LIST_IMEMODE_INACTIVE)
                return ImeMode.Inactive;
            else
            {
                throw new Exception(
                    String.Format("[ERROR], ToImeMode(): An ImeMode is expected but {0} is set",
                    input)
                    );
            }
        }

        public static LocalizedLabel ToEnglishLabel(string input)
        {
            return new LocalizedLabel(input, ENGLISH_LANGUAGE_CODE);
        }

        public static LocalizedLabel ToThaiLabel(string input)
        {
            return new LocalizedLabel(input, THAI_LANGUAGE_CODE);
        }
        #endregion

        #region Create Enity
        public static void CreateEntity(
            OrganizationServiceProxy serviceProxy,
            string entitySchemaName,
            string entityOwnershipType,
            string entityIsActivity,
            string entityDescription,
            string entityDisplayNameEN,
            string entityDisplayNameTH,
            string entityDisplayCollectionNameEN,
            string entityDisplayCollectionNameTH,

            string primaryAttributeSchema,
            string primaryAttributeRequiredLevel,
            string primaryAttributeMaxLength,
            string primaryAttributeFormat,
            string primaryAttributeDescription,
            string primaryAttributeDisplaynameEN,
            string primaryAttributeDisplaynameTH
        )
        {
            Label displayNameLabel;
            LocalizedLabel englishLabel;
            LocalizedLabel thaiLabel;

            #region Define the entity 
            EntityMetadata entity = new EntityMetadata();   
            
            //-- SchemaName --
            entity.SchemaName = entitySchemaName;

            //-- Description --
            entity.Description = new Label(entityDescription, ENGLISH_LANGUAGE_CODE);

            //-- OwnershipType --
            string OWNERSHIP = entityOwnershipType.ToUpper();

            if (OWNERSHIP == "USER")
                entity.OwnershipType = OwnershipTypes.UserOwned;
            else
                entity.OwnershipType = OwnershipTypes.OrganizationOwned;

            //-- IsActivity --
            string ISACTIVITY = entityIsActivity.ToUpper();

            if ((ISACTIVITY == "TRUE") || (ISACTIVITY == "T") || (ISACTIVITY == "YES") || (ISACTIVITY == "Y"))
                entity.IsActivity = true;
            else
                entity.IsActivity = false;

            //-- DisplayName --
            displayNameLabel = new Label();

            englishLabel = new LocalizedLabel(
                entityDisplayNameEN,
                ENGLISH_LANGUAGE_CODE
            );
            displayNameLabel.LocalizedLabels.Add(englishLabel);

            if (THAI_LANGUAGE_CODE != 0)
            {
                thaiLabel = new LocalizedLabel(
                    entityDisplayNameTH,
                    THAI_LANGUAGE_CODE
                );
                displayNameLabel.LocalizedLabels.Add(thaiLabel);
            }//-- End of if --

            entity.DisplayName = displayNameLabel;

            //-- DisplayCollectionName --
            displayNameLabel = new Label();

            englishLabel = new LocalizedLabel(
                entityDisplayCollectionNameEN,
                ENGLISH_LANGUAGE_CODE
            );
            displayNameLabel.LocalizedLabels.Add(englishLabel);

            if (THAI_LANGUAGE_CODE != 0)
            {
                thaiLabel = new LocalizedLabel(
                    entityDisplayNameTH,
                    THAI_LANGUAGE_CODE
                );
                displayNameLabel.LocalizedLabels.Add(thaiLabel);
            }//-- End of if --

            entity.DisplayCollectionName = displayNameLabel;

            //----------------------------------
            #endregion

            #region Define the primary attribute
            StringAttributeMetadata primaryAttribute = new StringAttributeMetadata();

            //-- Schema Name --
            primaryAttribute.SchemaName = primaryAttributeSchema;


            //-- Required Level --
            string REQUIREDLEVEL = primaryAttributeRequiredLevel.ToUpper();

            if (REQUIREDLEVEL == "NONE")
            {
                primaryAttribute.RequiredLevel =
                    new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None);
            }
            else if (REQUIREDLEVEL == "REQUIRED")
            {
                primaryAttribute.RequiredLevel =
                    new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.ApplicationRequired);
            }
            else if (REQUIREDLEVEL == "RECOMMENDED")
            {
                primaryAttribute.RequiredLevel =
                    new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.Recommended);
            }
            else
            {
                primaryAttribute.RequiredLevel =
                    new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.SystemRequired);
            }

            //-- Max Length --
            primaryAttribute.MaxLength = Convert.ToInt32(primaryAttributeMaxLength);

            //-- String Format --
            primaryAttribute.Format = StringFormat.Text;

            //-- Display Name --
            displayNameLabel = new Label();

            englishLabel = new LocalizedLabel(
                    primaryAttributeDisplaynameEN,
                    ENGLISH_LANGUAGE_CODE
                );
            displayNameLabel.LocalizedLabels.Add(englishLabel);

            if (THAI_LANGUAGE_CODE != 0)
            {
                thaiLabel = new LocalizedLabel(
                    entityDisplayNameTH,
                    THAI_LANGUAGE_CODE
                );
                displayNameLabel.LocalizedLabels.Add(thaiLabel);
            }//-- End of if --

            primaryAttribute.DisplayName = displayNameLabel;

            //-- Description --
            primaryAttribute.Description = new Label(primaryAttributeDescription, ENGLISH_LANGUAGE_CODE);

            //----------------------------------
            #endregion

            CreateEntityRequest createRequest = new CreateEntityRequest
            {
                Entity = entity,
                PrimaryAttribute = primaryAttribute
            };

            serviceProxy.Execute(createRequest);
        }//-- End of CreateEntity() --
        #endregion

        #region Delete Entity
        public static void DeleteEntity(OrganizationServiceProxy serviceProxy, string entitySchemaName)
        {
            DeleteEntityRequest request = new DeleteEntityRequest()
            {
                LogicalName = entitySchemaName,
            };

            serviceProxy.Execute(request);
        }//-- End of Delete Entity --
        #endregion

        #region Create Attributes By Type

        #region CreateASingleLineText()
        public static void CreateASingleLineText(
            OrganizationServiceProxy serviceProxy, 
            string entitySchemaName,
            string schemaName,
            string requiredLevel,
            string englishLabelText,
            string thaiLabelText,
            string description,
            string format,
            string maxLength
            )
        {
            StringAttributeMetadata attribute = new StringAttributeMetadata();

            attribute.SchemaName = schemaName;

            attribute.RequiredLevel = new AttributeRequiredLevelManagedProperty(
                ToAttributeRequiredLevelManagedProperty(requiredLevel)
                );

            attribute.MaxLength = AdaEntityUtils.ToInt(maxLength);
                    
            attribute.Format = AdaEntityUtils.ToStringFormat(format);

            //-- Create display name in Thai and English ------------------------------------
            Label displayNameLabel = new Label();

            displayNameLabel.LocalizedLabels.Add(AdaEntityUtils.ToEnglishLabel(englishLabelText));

            if (THAI_LANGUAGE_CODE != 0)
            {
                displayNameLabel.LocalizedLabels.Add(AdaEntityUtils.ToThaiLabel(thaiLabelText));
            }

            attribute.DisplayName = displayNameLabel;
            //---------------------------------------------------------------------------------

            attribute.Description = new Label(description, ENGLISH_LANGUAGE_CODE);

            //-- Create the request instance --
            CreateAttributeRequest createAttributeRequest = new CreateAttributeRequest
            {
                EntityName = entitySchemaName,
                Attribute = attribute
            };

            //-- Call SDK service to create the attribute --
            serviceProxy.Execute(createAttributeRequest);
        }//-- End of CreateASingleLineText() --
        #endregion

        #region CreateABoolean()
        public static void CreateABoolean(
            OrganizationServiceProxy serviceProxy,
            string entitySchemaName,
            string schemaName,
            string requiredLevel,
            string englishLabelText,
            string thaiLabelText,
            string description,
            string trueText,
            string falseText
            )
        {

            BooleanAttributeMetadata attribute = new BooleanAttributeMetadata();

            attribute.SchemaName = schemaName;

            //-- Create display name in Thai and English ------------------------------------
            Label displayNameLabel = new Label();

            displayNameLabel.LocalizedLabels.Add(AdaEntityUtils.ToEnglishLabel(englishLabelText));

            if (THAI_LANGUAGE_CODE != 0)
            {
                displayNameLabel.LocalizedLabels.Add(AdaEntityUtils.ToThaiLabel(thaiLabelText));
            }

            attribute.DisplayName = displayNameLabel;
            //---------------------------------------------------------------------------------

            attribute.RequiredLevel = new AttributeRequiredLevelManagedProperty(
                AdaEntityUtils.ToAttributeRequiredLevelManagedProperty(requiredLevel)
                );

            attribute.Description = new Label(description, ENGLISH_LANGUAGE_CODE);

            attribute.OptionSet = new BooleanOptionSetMetadata(
                    new OptionMetadata(new Label(trueText, ENGLISH_LANGUAGE_CODE), 1),
                    new OptionMetadata(new Label(falseText, ENGLISH_LANGUAGE_CODE), 0)
                );

            //-- Create the request instance --
            CreateAttributeRequest createAttributeRequest = new CreateAttributeRequest
            {
                EntityName = entitySchemaName,
                Attribute = attribute
            };

            //-- Call SDK service to create the attribute --
            serviceProxy.Execute(createAttributeRequest);
        }//-- End of CreateABoolean() --
        #endregion

        #region CreateADateTime()
        public static void CreateADateTime(
            OrganizationServiceProxy serviceProxy,
            string entitySchemaName,
            string schemaName,
            string requiredLevel,
            string englishLabelText,
            string thaiLabelText,
            string description,
            string format,
            string imeMode
            )
        {
            DateTimeAttributeMetadata attribute = new DateTimeAttributeMetadata();

            attribute.SchemaName = schemaName;

            //-- Create display name in Thai and English ------------------------------------
            Label displayNameLabel = new Label();

            displayNameLabel.LocalizedLabels.Add(AdaEntityUtils.ToEnglishLabel(englishLabelText));

            if (THAI_LANGUAGE_CODE != 0)
            {
                displayNameLabel.LocalizedLabels.Add(AdaEntityUtils.ToThaiLabel(thaiLabelText));
            }

            attribute.DisplayName = displayNameLabel;
            //---------------------------------------------------------------------------------

            attribute.RequiredLevel = new AttributeRequiredLevelManagedProperty(
                AdaEntityUtils.ToAttributeRequiredLevelManagedProperty(requiredLevel)
                );

            attribute.Description = new Label(description, ENGLISH_LANGUAGE_CODE);

            attribute.Format = AdaEntityUtils.ToDateTimeFormat(format);

            attribute.ImeMode = AdaEntityUtils.ToImeMode(imeMode);

            //-- Create the request instance --
            CreateAttributeRequest createAttributeRequest = new CreateAttributeRequest
            {
                EntityName = entitySchemaName,
                Attribute = attribute
            };

            //-- Call SDK service to create the attribute --
            serviceProxy.Execute(createAttributeRequest);
        }//-- End of CreateADateTime() --
        #endregion

        #region CreateAMoney()
        public static void CreateAMoney(
            OrganizationServiceProxy serviceProxy,
            string entitySchemaName,
            string schemaName,
            string requiredLevel,
            string englishLabelText,
            string thaiLabelText,
            string description,
            string maxValue,
            string minValue,
            string precision,
            string imeMode
            )
        {
            MoneyAttributeMetadata attribute = new MoneyAttributeMetadata();

            attribute.SchemaName = schemaName;

            //-- Create display name in Thai and English ------------------------------------
            Label displayNameLabel = new Label();

            displayNameLabel.LocalizedLabels.Add(AdaEntityUtils.ToEnglishLabel(englishLabelText));

            if (THAI_LANGUAGE_CODE != 0)
            {
                displayNameLabel.LocalizedLabels.Add(AdaEntityUtils.ToThaiLabel(thaiLabelText));
            }

            attribute.DisplayName = displayNameLabel;
            //---------------------------------------------------------------------------------

            attribute.RequiredLevel = new AttributeRequiredLevelManagedProperty(
                AdaEntityUtils.ToAttributeRequiredLevelManagedProperty(requiredLevel)
                );

            attribute.Description = new Label(description, ENGLISH_LANGUAGE_CODE);

            attribute.MaxValue = AdaEntityUtils.ToDouble(maxValue);

            attribute.MinValue = AdaEntityUtils.ToDouble(minValue);

            attribute.Precision = AdaEntityUtils.ToInt(precision);

            attribute.PrecisionSource = 1;

            attribute.ImeMode = AdaEntityUtils.ToImeMode(imeMode);

            //-- Create the request instance --
            CreateAttributeRequest createAttributeRequest = new CreateAttributeRequest
            {
                EntityName = entitySchemaName,
                Attribute = attribute
            };

            //-- Call SDK service to create the attribute --
            serviceProxy.Execute(createAttributeRequest);
        }//-- End of CreateAMoney() --
        #endregion

        #region CreateADecimal()
        public static void CreateADecimal(
            OrganizationServiceProxy serviceProxy,
            string entitySchemaName,
            string schemaName,
            string requiredLevel,
            string englishLabelText,
            string thaiLabelText,
            string description,
            string maxValue,
            string minValue,
            string precision
            )
        {

            DecimalAttributeMetadata attribute = new DecimalAttributeMetadata();

            attribute.SchemaName = schemaName;

            //-- Create display name in Thai and English ------------------------------------
            Label displayNameLabel = new Label();

            displayNameLabel.LocalizedLabels.Add(AdaEntityUtils.ToEnglishLabel(englishLabelText));

            if (THAI_LANGUAGE_CODE != 0)
            {
                displayNameLabel.LocalizedLabels.Add(AdaEntityUtils.ToThaiLabel(thaiLabelText));
            }

            attribute.DisplayName = displayNameLabel;
            //---------------------------------------------------------------------------------

            attribute.RequiredLevel = new AttributeRequiredLevelManagedProperty(
                AdaEntityUtils.ToAttributeRequiredLevelManagedProperty(requiredLevel)
                );

            attribute.Description = new Label(description, ENGLISH_LANGUAGE_CODE);

            attribute.MaxValue = (decimal)AdaEntityUtils.ToDouble(maxValue);

            attribute.MinValue = (decimal)AdaEntityUtils.ToDouble(minValue);

            attribute.Precision = AdaEntityUtils.ToInt(precision);

            //-- Create the request instance --
            CreateAttributeRequest createAttributeRequest = new CreateAttributeRequest
            {
                EntityName = entitySchemaName,
                Attribute = attribute
            };

            //-- Call SDK service to create the attribute --
            serviceProxy.Execute(createAttributeRequest);
        }//-- End of CreateAnAttribute() --
        #endregion

        #region CreateAnInteger()
        public static void CreateAnInteger(
            OrganizationServiceProxy serviceProxy,
            string entitySchemaName,
            string schemaName,
            string requiredLevel,
            string englishLabelText,
            string thaiLabelText,
            string description,
            string maxValue,
            string minValue
            )
        {
            IntegerAttributeMetadata attribute = new IntegerAttributeMetadata();

            attribute.SchemaName = schemaName;

            //-- Create display name in Thai and English ------------------------------------
            Label displayNameLabel = new Label();

            displayNameLabel.LocalizedLabels.Add(AdaEntityUtils.ToEnglishLabel(englishLabelText));

            if (THAI_LANGUAGE_CODE != 0)
            {
                displayNameLabel.LocalizedLabels.Add(AdaEntityUtils.ToThaiLabel(thaiLabelText));
            }

            attribute.DisplayName = displayNameLabel;
            //---------------------------------------------------------------------------------

            attribute.RequiredLevel = new AttributeRequiredLevelManagedProperty(
                AdaEntityUtils.ToAttributeRequiredLevelManagedProperty(requiredLevel)
                );

            attribute.Description = new Label(description, ENGLISH_LANGUAGE_CODE);

            attribute.Format = IntegerFormat.None; //-- The utility class is to be completed later --

            attribute.MaxValue = Convert.ToInt32(Math.Ceiling(AdaEntityUtils.ToDouble(maxValue)));

            attribute.MinValue = Convert.ToInt32(Math.Ceiling(AdaEntityUtils.ToDouble(minValue)));

            //-- Create the request instance --
            CreateAttributeRequest createAttributeRequest = new CreateAttributeRequest
            {
                EntityName = entitySchemaName,
                Attribute = attribute
            };

            //-- Call SDK service to create the attribute --
            serviceProxy.Execute(createAttributeRequest);
        }//-- End of CreateAnAttribute() --
        #endregion

        #region CreateAMemo()
        public static void CreateAMemo(
            OrganizationServiceProxy serviceProxy,
            string entitySchemaName,
            string schemaName,
            string requiredLevel,
            string englishLabelText,
            string thaiLabelText,
            string description,
            string format,
            string maxLength,
            string imeMode
            )
        {
            MemoAttributeMetadata attribute = new MemoAttributeMetadata();

            attribute.SchemaName = schemaName;

            //-- Create display name in Thai and English ------------------------------------
            Label displayNameLabel = new Label();

            displayNameLabel.LocalizedLabels.Add(AdaEntityUtils.ToEnglishLabel(englishLabelText));

            if (THAI_LANGUAGE_CODE != 0)
            {
                displayNameLabel.LocalizedLabels.Add(AdaEntityUtils.ToThaiLabel(thaiLabelText));
            }

            attribute.DisplayName = displayNameLabel;
            //---------------------------------------------------------------------------------

            attribute.RequiredLevel = new AttributeRequiredLevelManagedProperty(
                AdaEntityUtils.ToAttributeRequiredLevelManagedProperty(requiredLevel)
                );

            attribute.Description = new Label(description, ENGLISH_LANGUAGE_CODE);

            attribute.Format = AdaEntityUtils.ToStringFormat(format);

            attribute.MaxLength = AdaEntityUtils.ToInt(maxLength);

            attribute.ImeMode = AdaEntityUtils.ToImeMode(imeMode);

            //-- Create the request instance --
            CreateAttributeRequest createAttributeRequest = new CreateAttributeRequest
            {
                EntityName = entitySchemaName,
                Attribute = attribute
            };

            //-- Call SDK service to create the attribute --
            serviceProxy.Execute(createAttributeRequest);
        }//-- End of CreateAnAttribute() --
        #endregion

        #region CreateAGlobalOptionset()
        public static void CreateAGlobalOptionset(
            OrganizationServiceProxy serviceProxy,
            string entitySchemaName,
            string schemaName,
            string requiredLevel,
            string englishLabelText,
            string thaiLabelText,
            string description,
            string globalOptionsetName
            )
        {

            PicklistAttributeMetadata attribute = new PicklistAttributeMetadata();

            attribute.SchemaName = schemaName;
            attribute.LogicalName = schemaName;

            //-- Create display name in Thai and English ------------------------------------
            Label displayNameLabel = new Label();

            displayNameLabel.LocalizedLabels.Add(AdaEntityUtils.ToEnglishLabel(englishLabelText));

            if (THAI_LANGUAGE_CODE != 0)
            {
                displayNameLabel.LocalizedLabels.Add(AdaEntityUtils.ToThaiLabel(thaiLabelText));
            }

            attribute.DisplayName = displayNameLabel;
            //---------------------------------------------------------------------------------

            attribute.RequiredLevel = new AttributeRequiredLevelManagedProperty(
                AdaEntityUtils.ToAttributeRequiredLevelManagedProperty(requiredLevel)
                );

            attribute.Description = new Label(description, ENGLISH_LANGUAGE_CODE);

            attribute.OptionSet = new OptionSetMetadata{
                IsGlobal = true,
                Name = globalOptionsetName
            };

            //-- Create the request instance --
            CreateAttributeRequest createAttributeRequest = new CreateAttributeRequest
            {
                EntityName = entitySchemaName,
                Attribute = attribute
            };

            //-- Call SDK service to create the attribute --
            serviceProxy.Execute(createAttributeRequest);
        }//-- End of CreateAGlobalOptionset() --
        #endregion

        /*
        #region CreateAnOptionSet()
        z
        public static void CreateAnAttribute(OrganizationServiceProxy serviceProxy, int index, string entitySchemaName, AdaColumns attributes)
        {
            string attributeType = AdaEntityUtils.GetStringValue(index, attributes, HEADER_ATTRIBUTETYPE);

            if (attributeType != AdaEntityUtils.LIST_ATTRIBUTETYPE_)
            {
                throw new Exception(
                    String.Format("[ERROR], (): {0} is expected but {1} is set",
                    AdaEntityUtils.LIST_ATTRIBUTETYPE_,
                    attributeType)
                    );
            }

            XXXAttributeMetadata attribute = new XXXAttributeMetadata();

            attribute.SchemaName =
                AdaEntityUtils.GetStringValue(index, attributes, HEADER_SCHEMANAME);

            attribute.DisplayName =
                AdaEntityUtils.ToEnglishLabel(
                    AdaEntityUtils.GetStringValue(index, attributes, HEADER_DISPLAYNAME_EN)
                );

            attribute.RequiredLevel = new AttributeRequiredLevelManagedProperty(
                AdaEntityUtils.ToAttributeRequiredLevelManagedProperty(
                    AdaEntityUtils.GetStringValue(index, attributes, HEADER_REQUIREDLEVEL))
                );

            attribute.Description = //new Label("The name of the bank.", ENGLISH_LANGUAGE_CODE)
                AdaEntityUtils.ToEnglishLabel(
                    AdaEntityUtils.GetStringValue(index, attributes, HEADER_DESCRIPTION_EN)
                );

            //-- Create the request instance --
            CreateAttributeRequest createAttributeRequest = new CreateAttributeRequest
            {
                EntityName = entitySchemaName,
                Attribute = attribute
            };

            //-- Call SDK service to create the attribute --
            serviceProxy.Execute(createAttributeRequest);
        }//-- End of CreateAnAttribute() --

        #endregion
        
        */ 
        #endregion
       
    }//-- End of class AdaEntityUtils --
}//-- End of namespace --

//************************************************************************
// End of file
//************************************************************************
